﻿namespace MvcScriptManager
{
    using System.Configuration;
    using System.Security;
    using System.Security.Permissions;
    using System.Web.Configuration;

    /// <summary>
    /// Info about app level compilation.
    /// </summary>
    internal sealed class AppLevelCompilationSectionCache
    {
        private static readonly AppLevelCompilationSectionCache instance = new AppLevelCompilationSectionCache();
        private bool? debug;

        /// <summary>
        /// Initializes a new instance of the <see cref="AppLevelCompilationSectionCache"/> class.
        /// </summary>
        private AppLevelCompilationSectionCache()
        {
        }

        /// <summary>
        /// Gets the singleton instance.
        /// </summary>
        /// <value>The instance.</value>
        public static AppLevelCompilationSectionCache Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the app is under debug mode.
        /// </summary>
        /// <value><c>true</c> if under debug mode; otherwise, <c>false</c>.</value>
        public bool Debug
        {
            get
            {
                if (!this.debug.HasValue)
                {
                    this.debug = new bool?(GetDebugFromConfig());
                }

                return this.debug.Value;
            }
        }

        /// <summary>
        /// Gets the debug info from config.
        /// </summary>
        /// <returns>The debug info.</returns>
        [SecurityCritical, SecurityTreatAsSafe, ConfigurationPermission(SecurityAction.Assert, Unrestricted = true)]
        private static bool GetDebugFromConfig()
        {
            CompilationSection webApplicationSection = (CompilationSection)WebConfigurationManager.GetWebApplicationSection("system.web/compilation");
            return webApplicationSection.Debug;
        }
    }
}
